package com.hd.trans.files.model;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.hd.trans.db.bean.TransitionType;
import com.hd.trans.files.bean.Audio;
import com.hd.trans.files.bean.FilesBean;
import com.hd.trans.files.bean.Video;
import com.hd.trans.files.domain.RequestAudioDataUseCase;
import com.hd.trans.files.domain.RequestVideoDataUserCase;
import com.hd.trans.utils.SharedFileUtils;

import java.util.ArrayList;
import java.util.List;

public class MediaListViewModel extends BaseViewModel {

    // index: 0 媒体库，1 录音，2 剪辑
    private final int mTabIndex;
    //TransitionType
    private final int mTranslationType;
    // 记录上次搜索的关键词
    private String mLastKeyWord = "";
    // 音频原数据
    private final List<Audio> mAudioData = new ArrayList<>();
    // 视频原数据
    private final List<Video> mVideoData = new ArrayList<>();
    //文档原数据
    private final List<FilesBean> mFilesData = new ArrayList<>();
    // 音频数据
    private final MutableLiveData<List<Audio>> mAudioDataObserver = new MutableLiveData<>();
    // 视频数据
    private final MutableLiveData<List<Video>> mVideoDataObserver = new MutableLiveData<>();

    // 视频数据
    private final MutableLiveData<List<FilesBean>> mFilesDataObserver = new MutableLiveData<>();


    public MediaListViewModel(@NonNull Application application, int index, int translationType) {
        super(application);
        this.mTabIndex = index;
        this.mTranslationType = translationType;
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        String folder = "";
        if (mTabIndex == PickingFileViewModel.TAB_MEDIA_LIB) {
            folder = "";
        }
        else if (mTabIndex == PickingFileViewModel.TAB_MEDIA_QQ) {
            folder = "QQ";
        }
        else if (mTabIndex == PickingFileViewModel.TAB_MEDIA_WX) {
            folder = "WeiXin";
        }

        if (mTranslationType == TransitionType.DOCUMENT) {
            loadDocData(owner, folder);
        } else {
            loadAudioData(owner, folder);
        }
    }


    public MutableLiveData<List<Video>> getVideoDataObserver() {
        return mVideoDataObserver;
    }

    public MutableLiveData<List<Audio>> getAudioDataObserver() {
        return mAudioDataObserver;
    }

    public MutableLiveData<List<FilesBean>> getFilesDataObserver() {
        return mFilesDataObserver;
    }


    /**
     * 加载剪辑视频数据
     */
    /*private void loadEditVideoData(LifecycleOwner owner) {
        new RequestVideoDataUserCase()
                .addLifecycleOwner(owner)
                .addVideoDataObserver(mVideoDataObserver)
                .loadEditVideoData(getApplication());
    }*/

    /**
     * 加载媒体库视频数据
     */
    private void loadVideoData(LifecycleOwner owner) {
        new RequestVideoDataUserCase()
                .addLifecycleOwner(owner)
                .addVideoDataObserver(mVideoDataObserver)
                .loadVideoData(getApplication(), mVideoData::addAll);
    }

    /**
     * 加载媒体库音频数据
     */
    private void loadAudioData(LifecycleOwner owner, String folder) {
        new RequestAudioDataUseCase()
                .addLifecycleOwner(owner)
                .addAudioDataObserver(mAudioDataObserver)
                .loadAudioData(getApplication(), folder, mAudioData::addAll);
    }

    /**
     * 加载媒体库文档数据
     */
    private void loadDocData(LifecycleOwner owner, String folder) {
        new RequestAudioDataUseCase()
                .addLifecycleOwner(owner)
                .addAudioDataObserver(mAudioDataObserver)
                .loadDocData(getApplication(), folder, mAudioData::addAll);
    }

    /**
     * 通过关键搜索音频
     */
    public void audioSearch(String keyWord) {
       /* if (mFunction == Configs.TAG_SDK_VIDEO_EXTRACTION_AUDIO
                && (mTabIndex == 0 || mTabIndex == 2)) {
            new SearchVideoUseCase()
                    .addSearchVideoObserver(mVideoDataObserver)
                    .searchVideo(mVideoData, keyWord, mLastKeyWord, getSortModeItem(),
                            lastKeyWord -> mLastKeyWord = lastKeyWord);
        } else {
            new SearchAudioUseCase()
                    .addAudioDataObserver(mAudioDataObserver)
                    .audioSearch(mAudioData, keyWord, mLastKeyWord, getSortModeItem(),
                            lastKeyword -> mLastKeyWord = lastKeyword
                    );
        }*/
    }

    private FileSortModel.ModeItem getSortModeItem() {
        return SharedFileUtils.getInstance(getApplication()).getFileSortModeItem();
    }

    public static class MediaListViewModelFactory implements ViewModelProvider.Factory {

        private final Application mApplication;
        private final int mTabIndex;
        private final int mTranslationType;

        public MediaListViewModelFactory(Application application, int index, int translationType) {
            this.mTabIndex = index;
            this.mApplication = application;
            this.mTranslationType = translationType;
        }

        @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            return (T) new MediaListViewModel(mApplication, mTabIndex, mTranslationType);
        }
    }
}